﻿//账户管理-->存量管理
//Create By:Cui 2014年9月19日12:09:48

//1.0 加载JqueryDatabase
jQuery(function ($) {
	var oTable1 = $('#sample-table-2').dataTable({
		"aoColumns": [
		  { "bSortable": false },
		  { "bSortable": true },
		  { "bSortable": true },
		  { "bSortable": true },
		  { "bSortable": true },
		  { "bSortable": true },
		  { "bSortable": false },
		],
		oLanguage: {
			"sProcessing": "正在加载中......",
			"sLengthMenu": "每页显示 _MENU_ 条记录",
			"sZeroRecords": "对不起，查询不到相关数据！",
			"sEmptyTable": "表中无数据存在！",
			"sInfo": "当前显示： _START_ 到 _END_ 条，共 _TOTAL_ 条",
			"sInfoFiltered": "数据表中共为 _MAX_ 条记录",
			"sSearch": "",
			"oPaginate": {
				"sFirst": "首页",
				"sPrevious": "上一页",
				"sNext": "下一页",
				"sLast": "末页"
			}
		},
		iDisplayLength: 30,
		bLengthChange: false
	});


	$('table th input:checkbox').on('click', function () {
		var that = this;
		$(this).closest('table').find('tr > td:first-child input:checkbox')
		.each(function () {
			this.checked = that.checked;
			$(this).closest('tr').toggleClass('selected');
		});

	});


	$('[data-rel="tooltip"]').tooltip({ placement: tooltip_placement });
	function tooltip_placement(context, source) {
		var $source = $(source);
		var $parent = $source.closest('table')
		var off1 = $parent.offset();
		var w1 = $parent.width();

		var off2 = $source.offset();
		var w2 = $source.width();

		if (parseInt(off2.left) < parseInt(off1.left) + parseInt(w1 / 2)) return 'right';
		return 'left';
	}
})

window.onload = function () {

	//2.0 页面初始化
	//2.1 数量控件
	$('#count').ace_spinner({ value: 0, min: 1, max: 200, step: 1, btn_up_class: 'btn-info', btn_down_class: 'btn-info' })
				.on('change', function () {
				});

	$("td").addClass("tdBorder");

	//2.2 table表头上的样式
	$("#sample-table-2>thead tr>th").attr("style", "font-weight:bold;color:black;text-align:center");


	//2.3 table搜索样式
	$("#sample-table-2_filter label input").attr("placeholder", "搜索(多条件搜索中间用空格隔开)").attr("style", "width:50%");
	$("#sample-table-2_filter label").attr("style", "width:100%");
	$("#sample-table-2_filter").attr("style", "margin-right: -16px;");
	$("#sample-table-2_wrapper .row").first().children().eq(1).attr("style", "margin-top:-12px");
	$("#sample-table-2_wrapper").attr("style", "margin-top:-32px");
	$("#allotStore").attr("style", "margin-top:-4px");

	//2.4 增加表单行间距样式
	$("div .modal-body .row:not(#iconr)").attr("style", "margin-top:7px;").parent().attr("style", "margin-top:-26px");
	$("div .modal-body .row .col-sm-3").attr("style", "margin-top:6px;text-align:right");
	$("div#myModal .modal-body .row i:lt(6)").attr("style", "margin-top:9px;text-align:right;");

	$("#form_field_select_4_chosen").attr("style", "width:f100%");

	//2.5 显示页数一行的样式
	$("#sample-table-2").next().find(".col-sm-6").eq(0).attr("style", "margin-left: -12px;margin-top: 5px;");
	$("#sample-table-2").next().find(".col-sm-6").eq(1).attr("style", "margin-left: 12px;margin-top: 0px;");

	//2.6 按钮样式
	$("button.btn.btn-sm:gt(0)").attr("style", "margin:-5px 0px 0px 4px");

	//3     页面功能
	//3.1   存量分配
	function initForm() {
		//初始化指示灯
		$(".icon-lightbulb").attr("class", "icon-lightbulb tooltip-success cuitooltip form-success");
		//初始化文本框
		$("div#myModal .modal-body .row input").val("").removeAttr("style");
		//初始化下拉框
		if ($("#BillFrom").attr("disabled") == "disabled") {
			$("select:gt(1)").val("-1");
		}
		else {
			$("select").val("-1");
			$("select").eq(1).find("option:gt(0)").remove();
		}
		$("div#myModal .modal-body .row select").removeAttr("style");
		$(".ace-spinner").removeAttr("style");

		$("#PDescription").val("");
		$("#PDescription1").val("");

	}
	$("#allotStore").click(function () {
		initForm();
		$("#myModal").modal();
	});
	//3.2   刷新
	$("#buflush").click(function () {
		history.go(0);
	});
    //遮罩层div
	$("#bgdiv").attr("style", "display:none");
	//3.3   验证表单
	$("select").focus(function () {
		$(this).removeAttr("style");
		$(this).parent().next().attr("class", "icon-lightbulb tooltip-success cuitooltip form-success");
	});
	$("select").blur(function () {
		var obj = $(this).val();
		if (obj == "-1") {
			$(this).attr("style", " border-color: red;");
			$(this).parent().next().attr("class", "icon-lightbulb cuitooltip tooltip-error icon-animated-vertical red");
		}
	});

	//3.4   保存表单
	//3.5   新增保存
	$("#but_saveadd").click(function () {
		var BillFrom = $("#BillFrom").val();
		var BillTo = $("#BillTo").val();
		var productID = $("#productName").val();
		var count = $("#count").val();
		var Description = $("#Description").val();



		var flag = true;
		var msg = "";
		if (1 == 1) {

			if (parseInt(count) > Count) {
				flag = false;
				$("#count").attr("style", " border-color: red;");
				$("#count").parent().parent().parent().parent().find("i").eq(2).attr("class", "icon-lightbulb cuitooltip tooltip-error icon-animated-vertical red");
				msg = "填写的数量不能大于剩余数量";
			}
		}
		if (1 == 1) {
			if (productID == -1) {
				$("#productID").attr("style", " border-color: red;");
				$("#productID").parent().next().attr("class", "icon-lightbulb cuitooltip tooltip-error icon-animated-vertical red");
				msg = "请选择所充值的商品";
			}
		}
		if (1 == 1) {
			if (BillTo == -1) {
				$("#BillTo").attr("style", " border-color: red;");
				$("#BillTo").parent().next().attr("class", "icon-lightbulb cuitooltip tooltip-error icon-animated-vertical red");
				msg = "请选择目标客户";
			}
		}

		if (1 == 1) {
			if (BillFrom == -1) {
				$("#BillFrom").attr("style", " border-color: red;");
				$("#BillFrom").parent().next().attr("class", "icon-lightbulb cuitooltip tooltip-error icon-animated-vertical red");
				msg = "请选择来源客户";
			}
		}
		if (!flag) {
			$.gritter.add({
				title: '消息提醒',
				text: msg,
				class_name: 'gritter-info gritter-center'
			});
			return;
		}


		$.ajax({
			url: "AllotBill",
			dataType: "json",
			Type: "POST",
			data: {
				BillFrom: BillFrom,
				BillTo: BillTo,
				productID: productID,
				count: count,
				Description: Description
			},
			success: function (data) {
				var json = eval(data);
				if (json.result == "Success") {
					$("#but_infoclose").click();
					$.gritter.add({
						title: '消息提醒',
						text: json.message,
						class_name: 'gritter-info gritter-center'
					});

					history.go(0);
				}
				else {
					$.gritter.add({
						title: '消息提醒',
						text: json.message,
						class_name: 'gritter-info gritter-center'
					});
				}
			}
		});
	});

	

	//3.7   点击充值扣费
	function initForm1() {
		//初始化指示灯
		$(".icon-lightbulb").attr("class", "icon-lightbulb tooltip-success cuitooltip form-success");
		//初始化文本框
		$("div#myModal .modal-body .row input").val("").removeAttr("style");
		//初始化下拉框
		$("div#myModal .modal-body .row select").removeAttr("style");
		$(".ace-spinner").removeAttr("style");
		$("select:gt(0)").val("-1");
		$("#amount1").val("");

		$("#PDescription").val("");
		$("#PDescription1").val("");

	}
	$("#buyStore").click(function () {
		initForm1();
		$("#myModal1").modal();
	});

	$("#productName1").blur(function () {
		
		var Price1 = $("#productName1 option[value='" + $("#productName1").val() + "']").attr("price");
		PDescription = $("#productName1 option[value='" + $("#productName1").val() + "']").attr("Description");
		$("#amount1").val(Price1);
		$("#PDescription1").val(PDescription);

	})

	//3.8   充值新增保存
	$("#but_saveadd1").click(function () {
		var ToEC = $("#ToEC").val();
		var ProductID = $("#productName1").val();
		var Description = $("#Description").val();



		var flag = true;
		var msg = "";
		if (1 == 1) {
			if (ProductID == -1) {
				$("#productName1").attr("style", " border-color: red;");
				$("#productName1").parent().next().attr("class", "icon-lightbulb cuitooltip tooltip-error icon-animated-vertical red");
				msg = "请选择所充值的商品";
			}
		}
		if (1 == 1) {
			if (ToEC == -1) {
				$("#ToEC").attr("style", " border-color: red;");
				$("#ToEC").parent().next().attr("class", "icon-lightbulb cuitooltip tooltip-error icon-animated-vertical red");
				msg = "请选择充值对象";
			}
		}

		if (!flag) {
			$.gritter.add({
				title: '消息提醒',
				text: msg,
				class_name: 'gritter-info gritter-center'
			});
			return;
		}


		$.ajax({
			url: "BuyBill",
			dataType: "json",
			Type: "POST",
			data: {
				ToEC: ToEC,
				ProductID: ProductID,
				Description: Description
			},
			success: function (data) {
				var json = eval(data);
				if (json.result == "Success") {
					$("#but_infoclose").click();
					$.gritter.add({
						title: '消息提醒',
						text: json.message,
						class_name: 'gritter-info gritter-center'
					});

					history.go(0);
				}
				else {
					$.gritter.add({
						title: '消息提醒',
						text: json.message,
						class_name: 'gritter-info gritter-center'
					});
				}
			}
		});
	});



}




//来源客户和目标客户和产品 联动
$(function () {
	var FromIDBegin = "-1";
	$("#BillFrom").focus(function () {
		if ($("#BillFrom").attr("disabled") != "disabled") {
			FromIDBegin = $("#BillFrom").val();
		}
	});
	$("#BillFrom").blur(function () {
		if ($("#BillFrom").attr("disabled") != "disabled" && $("#BillFrom").val() != FromIDBegin) {
			$("select").eq(1).find("option:gt(0)").remove();
			$("select").eq(2).find("option:gt(0)").remove();
			$("select").eq(1).val("-1");
			$("select").eq(2).val("-1");
			$.ajax({
				url: "ChangeList",
				dataType: "Json",
				data: { id: $("#BillFrom").val() },
				success: function (data) {
					var json = eval(data);
					$("select").eq(1).find("option").after(json.message);

					$("select").eq(2).find("option").after(json.result);
				}
			});
		}
	});
});

//3.6   点击详情按钮
$(".showInfo").click(function () {
	var id = $(this).parent().parent().parent().children().eq(1).html();
	var productID = $(this).parent().parent().parent().children().eq(3).attr("productid");
	window.location.href = "BillInfo?id=" + id + "&productID=" + productID;
});



///////////////////////////////////////////////////////////////////////////////////////////////
//////////BEGIN///////////////添加存量表单的价格的一些控制方法/////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
var ProductID = "-1";
var Price = 0.00;
var Count = 0;


$("#productName").focus(function () {
	ProductID = $("#productName").val();
});

$("#productName").blur(function () {

	if ($("#productName").val() != ProductID) {
		$("#price").val("");
		$("#count").val("");
		$("#PDescription").val("");
		$("#PDescription1").val("");
		Price = $("#productName option[value='" + $("#productName").val() + "']").attr("price");
		Count = $("#productName option[value='" + $("#productName").val() + "']").attr("Count");
		PDescription = $("#productName option[value='" + $("#productName").val() + "']").attr("Description");
		$("#price").val(Price);
		$("#PDescription").val(PDescription);
	}
});
//更改数量同事变更价格
function changeCount() {
	$("#count").removeAttr("style");
	$("#count").parent().parent().parent().parent().find("i").eq(2).attr("class", "icon-lightbulb tooltip-success cuitooltip form-success");
	if (parseInt($("#count").val()) > Count) {
		$("#count").attr("style", " border-color: red;");
		$("#count").parent().parent().parent().parent().find("i").eq(2).attr("class", "icon-lightbulb cuitooltip tooltip-error icon-animated-vertical red");
		return;
	}
	//if ($("#price").val() == "") {
	//	$.gritter.add({
	//		title: '消息提醒',
	//		text: "请先选择产品！",
	//		class_name: 'gritter-info gritter-center'
	//	});
	////	return;
	//}
	//else {
	var p = parseFloat($("#price").val());
	var c = parseInt($("#count").val());
	$("#amount").val(Math.round((p * c) * 100) / 100);
	//}
}
///////////////////////////////////////////////////////////////////////////////////////////////
//////////END/////////////////添加存量表单的价格的一些控制方法/////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////